﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catch360.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

$link = mysql_connect("localhost","root","123456"); 
//$link = mysql_connect("localhost","root","coolchuan2012"); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('TYPEURLFOOTER','&os=17&model=HTC+Sensation&cpu=pyramid&order=download');
define('ONCECATCHCOUNT',20);


//$apps_url_page_header = "http://openbox.mobilem.360.cn/AppStore/getRecomendAppsBytype?type=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&startCount=21&start=0&count=".ONCECATCHCOUNT;
//$apps_url_app_all_list = "http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down&start=0&count=".ONCECATCHCOUNT;



start();


function start()
{
	//获取已存在app基础数据
	
	//应用分类
	$apptype_arr = getapptypelist();
	foreach ($apptype_arr as $apptype)
	{
		//每一类的url
		$apps_url_app_type_list = $apptype['url'].TYPEURLFOOTER;
		decodeurlandgetdata($apptype['id'],"soft",$apps_url_app_type_list);
	}
	//应用总榜
	decodeurlandgetdata(100,"soft","http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down");
	
//	decodeurlandgetdata(100,"soft","http://openbox.mobilem.360.cn/mintf/getAppsByCategory?cid=3&csid=11");
	
	
	//游戏分类
	$gametype_arr = getgametypelist();
	foreach ($gametype_arr as $gametype)
	{
		//每一类的url
		$apps_url_game_type_list = $gametype['url'].TYPEURLFOOTER;
		decodeurlandgetdata($apptype['id'],"game",$apps_url_game_type_list);
	}
	//游戏总榜
	decodeurlandgetdata(100,"game","http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=2&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down");

}









function decodeurlandgetdata($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	for($i=0;;)
	{
		$apps_url_app_all_list = $urlheader."&start=$i&count=".ONCECATCHCOUNT;
	
		@$json_string = file_get_html($apps_url_app_all_list);
		$obj=json_decode($json_string);

		//echo $obj->errno;
		//echo $obj->total;
		//echo $obj->end_state;
		
		
//		if($obj->data == "")
//		{
//			$i = $i + ONCECATCHCOUNT;
//			echo $i."++";continue;
//		}
//		$i = $i + ONCECATCHCOUNT;
//		continue;
		
		
		//抓取异常,结束
		if($obj->errno != 0)
		{
			break;
		}
		
		$appinfoarr = $obj->data;
		
		//本次为空，结束
		if(($obj->total == 0) || ($appinfoarr == ""))
		{
			break;
		}
		
		//有内容，解析
		$app_info_arr_insertdb_arr = catchinfodata($catchtype,$appbigtype,$i,$appinfoarr);
		
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			//判断是否需要更新信息库
			if(!searchisexit($app_info_arr_insertdb, $app_exit_infos))
			{
				insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
				//重新获取存在app信息，因为有了更新
				$app_exit_infos = get_exit_appinfo_fromdb();
			}
			
			update_appinfo_todb($app_info_arr_insertdb);
		}
		
		
		$i = $i + ONCECATCHCOUNT;
		
	//	结束
	//	if($obj->total < ONCECATCHCOUNT)
	//	{
	//		break;
	//	}
	}
}

function searchisexit($app_infos, $app_exit_info) 
{
	foreach ($app_exit_info as $app_exit) 
	{
		if ($app_infos['packagename'] == $app_exit["AppPackageName"]) 
		{
			return TRUE;
		}
	}
	return FALSE;
}




//排名分类，app大类，app信息
function catchinfodata($catchtype,$appbigtype,$flag,$appinfoarr)
{
	$ranking = $flag ;//* ONCECATCHCOUNT;
	$app_info_insertdb_arr = array();
	
	foreach ($appinfoarr as $appinfo)
	{
		$ranking++;
		$app_info_insertdb = array();
		$app_info_insertdb['name'] = $appinfo->name;
		$app_info_insertdb['packagename'] = $appinfo->apkid;
		$app_info_insertdb['AppDownTimes'] = $appinfo->download_times;
		$app_info_insertdb['Rating'] = $appinfo->rating;
		$app_info_insertdb['AppVersion'] = $appinfo->version_name;
		$app_info_insertdb['Versioncode'] = $appinfo->version_code;
		$app_info_insertdb['AppBaikeName'] = $appinfo->baike_name;
		$app_info_insertdb['AppLogo'] = $appinfo->logo_url;
		$app_info_insertdb['AppSize'] = $appinfo->size;
		
		$app_info_insertdb['AppType'] = "";
		if($catchtype != 100)
		{
			$app_info_insertdb['AppType'] = $catchtype;
		}
		$app_info_insertdb['AppBigType'] = $appbigtype;
		$app_info_insertdb['AppRanking'] = $ranking;
		$app_info_insertdb['AppRankingType'] = $catchtype;
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	
	return $app_info_insertdb_arr;
}






//抓取分类内容
//$apps_url_app_type_list = "http://openboxcdn.mobilem.360.cn/mintf/getcategorytagsinfobycid?cid=3&flag=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540";
//$json_string = file_get_html($apps_url_app_type_list);
//$obj=json_decode($json_string);
//echo $obj->errno;
//echo $obj->total;
//echo $obj->end_state;
//
//$apptypeinfoarr = $obj->data;
//foreach ($apptypeinfoarr as $apptypeinfo)
//{
//	echo ($apptypeinfo->name);
//	echo ($apptypeinfo->url);
//	echo "<br />";
//}



/*

$apptype_arr = getapptypelist();

foreach ($apptype_arr as $apptype)
{
	//每一类的url
	$apps_url_app_type_list = $apptype['url'].TYPEURLFOOTER.ONCECATCHCOUNT;

	$json_string = file_get_html($apps_url_app_type_list);
	
	$obj=json_decode($json_string);
	//var_dump($obj);
	//die();

	echo $obj->errno;
	echo $obj->total;
	echo $obj->end_state;
	
	
	$appinfoarr = $obj->data;
	
	foreach ($appinfoarr as $appinfo)
	{
		echo ($appinfo->name);echo "++";
		echo ($appinfo->apkid);echo "++";
		echo ($appinfo->download_times);echo "++";
		echo ($appinfo->rating);echo "++";
		echo ($appinfo->version_name);echo "++";
		echo ($appinfo->version_code);echo "++";
		echo ($appinfo->baike_name);echo "++";
		echo "<br />";
	}
	
}
*/


//
//http://openbox.mobilem.360.cn/AppStore/getRecomendAppsBytype?type=2&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&startCount=22&start=0&count=30
//http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=2&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down&start=0&count=20
//http://openboxcdn.mobilem.360.cn/mintf/getcategorytagsinfobycid?cid=4&flag=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540

/*

$apps_url_game_all_list = "http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=2&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down&start=0&count=".ONCECATCHCOUNT;

$json_string = file_get_html($apps_url_game_all_list);

$obj=json_decode($json_string);
//var_dump($obj);
//die();

echo $obj->errno;
echo $obj->total;
echo $obj->end_state;
$appinfoarr = $obj->data;
foreach ($appinfoarr as $appinfo)
{
//	var_dump($appinfo->apkid);
//	echo "<br />";
}


//$apps_url_app_type_list = "http://openboxcdn.mobilem.360.cn/mintf/getcategorytagsinfobycid?cid=4&flag=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540";
//$json_string = file_get_html($apps_url_app_type_list);
//$obj=json_decode($json_string);
//echo $obj->errno;
//echo $obj->total;
//echo $obj->end_state;
//
//$apptypeinfoarr = $obj->data;
//foreach ($apptypeinfoarr as $apptypeinfo)
//{
//	echo ($apptypeinfo->name);
//	echo ($apptypeinfo->url);
//	echo "<br />";
//}



$gametype_arr = getgametypelist();

foreach ($gametype_arr as $gametype)
{
	//每一类的url
	$apps_url_game_type_list = $gametype['url'].TYPEURLFOOTER.ONCECATCHCOUNT;

	$json_string = file_get_html($apps_url_game_type_list);
	
	$obj=json_decode($json_string);
	//var_dump($obj);
	//die();

	echo $obj->errno;
	echo $obj->total;
	echo $obj->end_state;
	
	
	$appinfoarr = $obj->data;
	
	foreach ($appinfoarr as $appinfo)
	{
		echo ($appinfo->name);echo "++";
		echo ($appinfo->apkid);echo "++";
		echo ($appinfo->download_times);echo "++";
		echo ($appinfo->rating);echo "++";
		echo ($appinfo->version_name);echo "++";
		echo ($appinfo->version_code);echo "++";
		echo ($appinfo->baike_name);echo "++";
		echo "<br />";
	}
	
}



die();

*/



/*
$apps_url_page_header = "http://apk.gfan.com/games_8_1_";
$apps_url_page_footer = ".html";
*/

function get_exit_appinfo_fromdb()
{
	$link = mysql_connect("localhost","root","123456"); 
	//$link = mysql_connect("localhost","root","coolchuan2012"); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select AppPackageName,AppName from 360_apps_info_all where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
	  	$app_exit_info_temp['AppPackageName'] = $row['AppPackageName'];
	  	$app_exit_info_temp['AppName'] = $row['AppName'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}


function insert_appinfo_ifnotexit_todb($app_info)
{
	$link = mysql_connect("localhost","root","123456"); 
	//$link = mysql_connect("localhost","root","coolchuan2012"); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into 360_apps_info_all (FCreateTime,AppName,AppPackageName,AppSize,AppType,AppBigType,AppLogo)
	 values ('.$createtime.',"'.$app_info['name'].'","'.$app_info['packagename'].'","'.$app_info['AppSize'].'","'.$app_info['AppType'].'","'.$app_info['AppBigType'].'","'.$app_info['AppLogo'].'")'; 

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = 'logs/360_info_success.log';
	$filename_fail = 'logs/360_info_fail.log';
	$filename_db_fail = 'logs/360_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$q."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}

function update_appinfo_todb($app_info)
{
	$link = mysql_connect("localhost","root","123456"); 
	//$link = mysql_connect("localhost","root","coolchuan2012"); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	
//	$app_info_insertdb['name'] = $appinfo->name;
//	$app_info_insertdb['packagename'] = $appinfo->apkid;
//	$app_info_insertdb['AppDownTimes'] = $appinfo->download_times;
//	$app_info_insertdb['Rating'] = $appinfo->rating;
//	$app_info_insertdb['AppVersion'] = $appinfo->version_name;
//	$app_info_insertdb['Versioncode'] = $appinfo->version_code;
//	$app_info_insertdb['AppBaikeName'] = $appinfo->baike_name;
//	$app_info_insertdb['AppType'] = "";
//	if($catchtype != 100)
//	{
//		$app_info_insertdb['AppType'] = $catchtype;
//	}
//	$app_info_insertdb['AppBigType'] = $appbigtype;
//	$app_info_insertdb['AppRanking'] = $ranking;
//	$app_info_insertdb['AppRankingType'] = $catchtype;
	
	
	
	
	
	
	$sql = 'insert into 360_apps_catch_data (FCreateTime,AppDownTimes,AppPackageName,Rating,AppVersion,Versioncode,AppRanking,AppRankingType)
	 values ('.$createtime.','.$app_info['AppDownTimes'].',"'.$app_info['packagename'].'","'.$app_info['Rating'].'","'.$app_info['AppVersion'].'","'.$app_info['Versioncode'].'",
	 '.$app_info['AppRanking'].','.$app_info['AppRankingType'].')'; 

	//echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = 'logs/360_success.log';
	$filename_fail = 'logs/360_fail.log';
	$filename_db_fail = 'logs/360_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$q."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}









?>